get_context_data, get_queryset { django } { ListView }


get_queryset

ListView에서 객체의 리스트를 리턴하는 메서드이다. 기본적으로는 objects.all()을 호출하지만 오버라이딩 하여 원하는 결과만을 필터링하거나 exclude 하여 리턴할 수 있다.

get_context_data

You will probably be overriding this method often to add things to display in your templates

주로 템플릿 컨텍스트(dict)에 원하는 엔트리를 추가하고자 할 때 사용한다.

def get_context_data(self, **kwargs):
    data = super().get_context_data(**kwargs)
    data['page_title'] = 'Authors'
    return data

pate_title을 사용할 수 있게 되었다

<h1>{{ page_title }}</h1>

<ul>
{% for author in author_list %}
    <li>{{ author.name }}</li>
{% endfor %}
</ul>